package com.demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import java.util.Set;

/**
 * Created by Administrator on 2017/7/6.
 */
public class WebViewOperation extends BasicTestCase{

        @Test
    public void testName() throws Exception {

        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.netease.newsreader.activity:id/bfw")));
        //by xpath 定位
        WebElement newsItem = driver.findElements(By.id("com.netease.newsreader.activity:id/dz")).get(1);
        newsItem.click();

        WebDriverWait wait2 = new WebDriverWait(driver,10);
        wait2.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.netease.newsreader.activity:id/ani")));
        Set<String> handles = driver.getContextHandles();
        for (String handle : handles){
            System.out.println("=========== "+handle);
        }
        //handles是个集合，把handles转成数组 toArray length代表大小，length-1 代表 取到最后一位
        String last = handles.toArray()[handles.toArray().length-1].toString();
        //判断 last 包含 WEBVIEW这些字眼
        if (last.contains("WEBVIEW")){
            //切换到 last上下文
            driver.context(last);
        }
        //通过 上下文 定位
        WebElement title = driver.findElement(By.xpath("//*[@id=\"read\"/article/h2/b]"));
        //WebView 只能通过这种方式定位
        System.out.println(title.getText().toString());

    }

}
